var isover=0;
var username;//用户中文名
var currentIndex=1;//页码
var myurl="";
var expUrl=myexpurl;//我的经验
var queUrl=myquestionurl;//我的悬赏
var newsUrl=mynewsurl;//我的资讯
var resUrl="https://www.pulu.com/webapi/api/puluquestion/GetMyzyProc_e";//我的资源
var titleWord="";
if(/qtype=1/.test(location.href)){
    myurl=newsUrl;
    titleWord="的资讯";
}else if(/qtype=2/.test(location.href)){
    myurl=queUrl;
    titleWord="的悬赏";
}else if(/qtype=3/.test(location.href)){
    myurl=expUrl;
    titleWord="的经验";
}else if(/qtype=4/.test(location.href)){
    myurl=resUrl;
    titleWord="的资源";
}

if(/otherid=[0-9a-z]+/.test(location.href)){
    var id=(location.href).match(/otherid=[0-9a-z]+/)[0].split("=")[1];
}else{
    history.go(-1);
}
$().ready(function(){
    $.ajax({
        type: "post",
        datatype: "json",
        url: userurl,
        data:{"userId":id},
        success:function(data){
            username=data.Data.userNick;
            $(".page_title").html(username+titleWord);
            aj();
        }
    });
});
$(".mweb_modal_back_button").click(function(){
    history.go(-1);
});
//
function godetail(i){
    if($.cookie("MYID")){
        if(/qtype=1/.test(location.href)){
            var errorUrl=/环球网|1905电影网|中国会计视野|编织人生网|IT之家/;
            if(i.dataset.sitename){
                if(errorUrl.test(i.dataset.sitename)){
                    location.href="http://www.pulu.com/weixin/newsdetail.html?newsid="+i.dataset.id+"#fromuser="+$.cookie("MYID");
                }else{
                    location.href="newsdetail.html?newsid="+i.dataset.id+"#fromuser="+$.cookie("MYID");
                }
            }
        }else if(/qtype=2/.test(location.href)){
            location.href="questiondetail.html?qid="+i.dataset.id+"#fromuser="+$.cookie("MYID");
        }else{
            location.href="shareexppage.html?eid="+i.dataset.id+"#fromuser="+$.cookie("MYID");
        }
    }else{
        location.href="http://www.pulu.com/weixin/index.html#fromuser="+id;
    }

}

var ajaxrunning="yes";//初始状态不可以滚动
function aj(){
    if(isover===0){
        ajaxrunning="yes";
        $.ajax({
            type:"get",
            datatype: "json",
            url: myurl+"?userId="+id+"&pageSize=15&currentIndex="+currentIndex,
            success:function(d){
                console.log(d);
                if(d.Message==="没有数据"){
                    isover=1;//没有数据了
                    $("#waiting").css("display","none");
                    $("#nonews").css("display","block");
                }else{
                    if(d.Data&&d.Data.length>0){
                        if(d.Data.length<15){
                            $("#waiting").css("display","none");
                            $("#nonews").css("display","block");
                        }
                        var num=0;
                        for(var i=0;i<d.Data.length;i++){
                            num++;
                            if(num===d.Data.length){
                                ajaxrunning="no";
                            }
                            if(d.Data[i].title){
                                var jname1="";
                                var reg2=/[\u4E00-\u9FA5]*/;
                                //昵称
                                var unick1="";
                                if(d.Data[i].shareUserNick){
                                    unick1=d.Data[i].shareUserNick
                                }else if(d.Data[i].userNick){
                                    unick1=d.Data[i].userNick;
                                }
                                //浏览数
                                var count=0;
                                if(d.Data[i].browseCount){count=d.Data[i].browseCount;}
                                else if(d.Data[i].count){count=d.Data[i].count;}
                                //判断是否有点赞者
                                var userimg="";//评星
                                userimg="<div class='starimg'><i style='width:"+parseFloat(d.Data[i].star)*20+"%;'></i></div>";
                                //高额
                                var isheilogo="";
                                var expire="";
                                if(d.Data[i].isHigh===1||d.Data[i].isHigh===2){
                                    isheilogo="<div class='isheighlogo'></div>";
                                    expire="<span class='daynum'>有效期"+d.Data[i].expireDay+"天</span><span>预期总收入"+parseFloat(d.Data[i].totalamount)/100+"元</span>";
                                }
                                //时间
                                if(d.Data[i].cTime){
                                    var tim=d.Data[i].cTime.match(/([0-9\-]+)/)[0];
                                    tim=getDateDiff(getDateTimeStamp(d.Data[i].cTime));
                                }else if(d.Data[i].shareTime){
                                    var tim=d.Data[i].shareTime.match(/([0-9\-]+)/)[0];
                                    tim=getDateDiff(getDateTimeStamp(d.Data[i].shareTime));
                                };
                                //匿名
                                var nimingimg="";
                                if(d.Data[i].isHideName===1){
                                    nimingimg="<img class='rt' src='img/niming.png'>"
                                }
                                //金额
                                var money="";//有没有钱
                                var tit=codetoface(d.Data[i].title);
                                if(d.Data[i].score>0){
                                    var score= parseFloat(d.Data[i].score)/100;
                                    money="<span class='havemy'><img src='img/money04.png'>"+score+"</span>";
                                }
                                //是不是转换
                                var isconvert="";
                                if(d.Data[i].isconvert&&d.Data[i].isconvert===1){
                                    isconvert="&nbsp;&nbsp;(悬赏转经验)"
                                }
                                //新闻网址
                                var sitename="";
                                if(d.Data[i].siteName){
                                    sitename="data-sitename='"+d.Data[i].siteName+"'";
                                }
                                //id
                                var thisis="";
                                if(d.Data[i].qid){thisis=d.Data[i].qid}
                                else if(d.Data[i].id){thisis=d.Data[i].id};
                                $("#questionlist").append("<div class='pagedlist_item shareexperience'><div class='feed_item feed_type_question'><div class='EventHeader'><span class='senduser'>"+unick1+"</span><span class='job'>·</span>"+tim+money+isheilogo+nimingimg+isconvert+"</div><h2><a class='question_link' "+sitename+" data-id='"+thisis+"' onclick='godetail(this)'>"+tit+"</a></h2><div class='upviter' style='display:none;'>"+"</div>"+expire+"<div class='ActionBar Question'> <div class='action_bar_inner'><div class='action_item'> <a class='ReadMoreActionItem'>浏览 "+count+"</a> </div> </div></div></div></div>");
                            }
                        };
                        $(".page_title").html();
                    }
                    else{
                        $("#waiting").css("display","none");
                        $("#nonews").css("display","block");
                    }
                }
            }
        })
    }
}
$(window).scroll(function(){
    var mainheight=$(".home_feed").height();
    var windowheight=$(window).height();
    var chajuli=$(".home_feed").offset().top - $(window).scrollTop();
    var cz=windowheight-mainheight+50;
    if(chajuli<cz&&ajaxrunning==="no"){
        console.log(ajaxrunning);
        currentIndex++;
        aj();
    }
});